package cn.changeforyou.analyzer.xmlparse.mybatis.model;

import cn.changeforyou.analyzer.CombineElement;
import cn.changeforyou.analyzer.combine.orm.compare.ComparedStruct;
import cn.changeforyou.analyzer.naming.NamingStrategy;
import java.util.Collections;
import java.util.stream.Collectors;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.dom4j.tree.DefaultElement;

import java.util.List;

/**
 * @author zhyu
 */
@Data
public class ResultMapLabel extends MybatisXmlLabel<DefaultElement> {
	private String type;
	private List<ResultTag> resultTags;
	
	@Override
	public List<String> getComparedStruct() {
		List<String> list = resultTags.stream().map(x -> x.getName(getNamingStrategy())).collect(Collectors.toList());
		Collections.sort(list);
		return list;
	}
	
	@Override
	public NamingStrategy getNamingStrategy() {
		return NamingStrategy.camelCase;
	}
	
	@Override
	public String getStructName() {
		return getTableName();
	}
	
	@Getter@Setter
	public static class ResultTag implements Comparable<ResultTag>, CombineElement {
		private String column;
		private String property;
		private DefaultElement node;
		private ResultMapLabel resultMapLabel;
		
		public ResultTag(ResultMapLabel resultMapLabel) {
			this.resultMapLabel = resultMapLabel;
		}
		
		@Override
		public int compareTo(ResultTag o) {
			return this.column.compareTo(o.column);
		}
		
		@Override
		public String getName() {
			return property;
		}
		
		@Override
		public ComparedStruct getParentStruct() {
			return resultMapLabel;
		}
		
		@Override
		public String toString() {
			return "ResultTag{" +
				       "column='" + column + '\'' +
				       ", property='" + property + '\'' +
				       ", resultMapLabel=@" + hashCode() +
				       '}';
		}
	}
}
